package cn.santi.controller;

import cn.santi.domain.*;
import cn.santi.emuns.R;
import cn.santi.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static cn.santi.utli.DateTool.gettime;

@RestController
@RequestMapping("/pay")
public class PayController {
    @Autowired
    private payService ps;
    @Autowired
    private SUserService ss;
    @Autowired
    private recordService rs;
    @Autowired
    private MycouponService ms;
    @Autowired
    private couponService cs;
   //显示充值套餐
    @GetMapping("/querypay")
    public R querypay(){
        QueryWrapper<pay> wrapper = new QueryWrapper<>();
        wrapper.eq("state",0);
        return R.ok(ps.list(wrapper));
    }
    //充值并添加记录
    private Map<Integer, Long> lastRequestTimeMap = new ConcurrentHashMap<>();

    @GetMapping("/addrecord")
    public R addrecord(int uid, String price) {
        long currentTime = System.currentTimeMillis();
        Long lastRequestTime = lastRequestTimeMap.get(uid);

        if (lastRequestTime != null && (currentTime - lastRequestTime) < 2000) {
            return R.fail("操作太频繁，请稍后再试");
        }

        lastRequestTimeMap.put(uid, currentTime);

        QueryWrapper<SUser> wrapper = new QueryWrapper<>();
        wrapper.eq("id", uid);
        SUser one = ss.getOne(wrapper);
        if (one == null) {
            return R.fail("用户不存在");
        }
        BigDecimal balance = one.getBalance();
        BigDecimal newPrice = new BigDecimal(price);
        balance = balance.add(newPrice);
        one.setBalance(balance);
        ss.updateById(one);
        record record = new record();
        record.setUid(uid);
        record.setType("0");
        record.setPrice("+"+" "+price);
        record.setCreatetime("充值到余额");
        record.setDate(gettime());
        rs.save(record);
        return R.ok(balance);
    }
    //显示充值记录
    @GetMapping("/queryrecordbyid")
    public R queryrecordbyid(int id){
        QueryWrapper<record> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",id).orderByAsc("date");
        return R.ok(rs.list(wrapper));
    }
    //购买优惠卷
    @GetMapping("/addmycoupon")
    public R addmycoupon(int uid,int id,String price){
        long currentTime = System.currentTimeMillis();
        Long lastRequestTime = lastRequestTimeMap.get(uid);

        if (lastRequestTime != null && (currentTime - lastRequestTime) < 2000) {
            return R.fail(250,"操作太频繁，请稍后再试");
        }

        lastRequestTimeMap.put(uid, currentTime);
        //查询用户金额和修改金额
        QueryWrapper<SUser> wrapper = new QueryWrapper<>();
        wrapper.eq("id",uid);
        SUser one = ss.getOne(wrapper);
        BigDecimal priceStr = new BigDecimal(price);
        BigDecimal userprice = one.getBalance();
        if (priceStr.compareTo(userprice) >= 0) {
            return R.fail(300,"余额不足");
        }
        BigDecimal  pr= userprice.subtract(priceStr);
        one.setBalance(pr);
        ss.updateById(one);
        //创建消费记录
        record record = new record();
        record.setUid(uid);
        record.setDate(gettime());
        record.setType("1");
        record.setPrice("-"+" "+price);
        record.setCreatetime("购买优惠卷消费");
        rs.save(record);
        //添加到用户优惠卷
        Mycoupon mycoupon = new Mycoupon();
        mycoupon.setDate(gettime());
        mycoupon.setUid(uid);
        mycoupon.setCid(id);
        mycoupon.setStatue("0");
        ms.save(mycoupon);
        return R.ok(pr,"购买成功");
    }
    //显示卡卷
    @GetMapping("/queryMycoupon")
    public R queryMycoupon(int uid){
        QueryWrapper<Mycoupon> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",uid);
        return R.ok(ms.list(wrapper));
    }
    //查询优惠卷querybycoupon
    @GetMapping("/querybycoupon")
    public R querybycoupon(int id){
        QueryWrapper<coupon> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        return R.ok(cs.getOne(wrapper));
    }
}
